﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using NTQOnlineExam.Services.Infrastructure;

namespace NTQOnlineExam.Services.DBCommand.Account
{
    public class UpdateUserInfoByUsernameDbCommand : ICommand<bool>
    {
        private readonly string _username;
        private readonly string _firstName;
        private readonly string _lastName;
        private readonly string _email;
        private readonly string _phone;
        private readonly string _address;
        private readonly string _poBox;
        private readonly string _zipCode;
        private readonly string _city;
        private readonly string _country;

        public UpdateUserInfoByUsernameDbCommand(string username, string firstName, string lastName, string email,
            string phone, string address, string poBox, string zipCode, string city, string country)
        {
            _username = username;
            _firstName = firstName;
            _lastName = lastName;
            _email = email;
            _phone = phone;
            _address = address;
            _poBox = poBox;
            _zipCode = zipCode;
            _city = city;
            _country = country;
        }

        public bool Execute()
        {
            var connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

            using (var con = new SqlConnection(connectionString))
            {
                using (var cmd = new SqlCommand("[UpdateUserInfo]", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Username", SqlDbType.NVarChar).Value = _username;
                    cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = _firstName;
                    cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = _lastName;
                    cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = _email;
                    cmd.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = (object)_phone ?? DBNull.Value;

                    cmd.Parameters.Add("@BillingAddress", SqlDbType.NVarChar).Value = (object)_address ?? DBNull.Value;
                    cmd.Parameters.Add("@BillingPOBox", SqlDbType.NVarChar).Value = (object)_poBox ?? DBNull.Value;
                    cmd.Parameters.Add("@BillingZipCode", SqlDbType.NVarChar).Value = (object)_zipCode ?? DBNull.Value;
                    cmd.Parameters.Add("@BillingCity", SqlDbType.NVarChar).Value = (object)_city ?? DBNull.Value;
                    cmd.Parameters.Add("@BillingCountry", SqlDbType.NVarChar).Value = (object)_country ?? DBNull.Value;

                    con.Open();
                    cmd.ExecuteNonQuery();
                }
            }
            return true;

        }
    }
}


